package app.itgungnir.compose.wan.business.main.mine

import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import app.itgungnir.compose.wan.business.main.MainViewModel
import app.itgungnir.compose.wan.utils.LocalDataStore
import app.itgungnir.compose.wan.widgets.WanAlertDialog
import app.itgungnir.compose.wan.widgets.WanScaffold

@Composable
fun MineScreen(navController: NavController = rememberNavController(), parentViewModel: MainViewModel = viewModel()) {
    val viewModel = viewModel<MineViewModel>()
    val unreadMsgCount by parentViewModel.unreadMsgCountState.collectAsState()
    // UI
    val logoutDialogState = remember { mutableStateOf(false) }
    WanScaffold(
        showStatusBar = true,
        navController = navController,
        errorState = viewModel.errorState.collectAsState(initial = null)
    ) {
        MineUI(
            navController = navController,
            hasLoginState = viewModel.hasLoginFlow.collectAsState(initial = false),
            userInfoState = viewModel.userInfoFlow.collectAsState(initial = null),
            coinInfoState = viewModel.coinInfoFlow.collectAsState(initial = null),
            onLogout = {
                viewModel.dismissDialogState.value = false
                logoutDialogState.value = true
            },
            unreadMsgCount = unreadMsgCount
        )
    }
    val dismissState = viewModel.dismissDialogState.collectAsState()
    WanAlertDialog(
        state = logoutDialogState,
        title = "提示",
        message = "是否确定退出登录？",
        loadingState = viewModel.loadingState.collectAsState(),
        loadingJob = viewModel.loadingJob,
        dismissState = dismissState,
        errorState = viewModel.dialogErrorState.collectAsState(initial = null),
        negativeText = "再想想",
        negativeCallback = { logoutDialogState.value = false },
        positiveText = "确定",
        positiveCallback = { viewModel.logout() }
    )
    // State observation
    LoginSuccessObservation(viewModel = viewModel, navController = navController, parentViewModel = parentViewModel)
    ReadMessageObservation(navController = navController, parentViewModel = parentViewModel)
}

@Composable
private fun LoginSuccessObservation(
    viewModel: MineViewModel = viewModel(),
    navController: NavController = rememberNavController(),
    parentViewModel: MainViewModel = viewModel()
) {
    val userLoginSuccess = navController.currentBackStackEntry?.savedStateHandle
        ?.getLiveData<Boolean>("user_has_login")?.observeAsState()
    LaunchedEffect(key1 = userLoginSuccess?.value == true && LocalDataStore.localData.hasLogin) {
        if (userLoginSuccess?.value == true && LocalDataStore.localData.hasLogin) {
            viewModel.getUserInfo()
            parentViewModel.getUnreadMessageCount()
            navController.currentBackStackEntry?.savedStateHandle?.set("user_has_login", false)
        }
    }
}

@Composable
private fun ReadMessageObservation(
    navController: NavController = rememberNavController(),
    parentViewModel: MainViewModel = viewModel()
) {
    val userReadMessage = navController.currentBackStackEntry?.savedStateHandle
        ?.getLiveData<Boolean>("user_read_message")?.observeAsState()
    LaunchedEffect(key1 = userReadMessage?.value == true && LocalDataStore.localData.hasLogin) {
        if (userReadMessage?.value == true && LocalDataStore.localData.hasLogin) {
            parentViewModel.getUnreadMessageCount()
            navController.currentBackStackEntry?.savedStateHandle?.set("user_read_message", false)
        }
    }
}
